﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Rhomble.Data.Users;

namespace Rhomble.Data
{
	public abstract class UserOnlyRepository : BaseRepository
	{
		private UserRepository _userRepo;
		private User _user;
		private string _userName;

		public UserOnlyRepository(string userName)
		{
			if (String.IsNullOrWhiteSpace(userName))
			{
				throw new InvalidOperationException("A valid user must be provided.");
			}

			_userName = userName;
		}

		protected User GetCurrentUser()
		{
			if (_user != null && _user.LoweredUserName == _userName.ToLower())
			{
				return _user;
			}

			if (_userRepo == null)
			{
				_userRepo = new UserRepository();
			}

			var user = _userRepo.GetQuery()
						.WithName(_userName)
						.Single();

			return user;
		}
	}
}
